home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / SpriteWorld 2.0 ƒ / SpriteWorld Examples / SpriteTest / About.c next >
Encoding:
Text File  |  1996-10-05  |  8.1 KB  |  346 lines  |  [TEXT/KAHL]

  1. ///--------------------------------------------------------------------------------------
  2. //    About.c
  3. //
  4. //    By:        Tony Myles
  5. //
  6. //    Copyright: © 1993-94 Tony Myles, All rights reserved worldwide.
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <SWIncludes.h>            // Automatically include all SpriteWorld.h files
  11.  
  12. #ifndef __DIALOGUTILS__
  13. #include "DialogUtils.h"
  14. #endif
  15.  
  16. #ifndef __SPRITETEST__
  17. #include "SpriteTest.h"
  18. #endif
  19.  
  20. #ifndef __APPLICATION__
  21. #include "Application.h"
  22. #endif
  23.  
  24. #ifndef __ABOUT__
  25. #include "About.h"
  26. #endif
  27.  
  28.  
  29. extern SpriteTestPtr gSpriteTestP;
  30.  
  31.  
  32. ///--------------------------------------------------------------------------------------
  33. // DisplayAboutBox
  34. ///--------------------------------------------------------------------------------------
  35.  
  36. void DisplayAboutBox(void)
  37. {
  38.     OSErr                 err;
  39.     GrafPtr             savePort;
  40.     DialogPtr             aboutDialogP;
  41.     SpriteWorldPtr         spriteWorldP = NULL;
  42.     SpriteLayerPtr         spriteLayerP = NULL;
  43.     SpritePtr             earthSpriteP = NULL; 
  44.     SpritePtr            shadowSpriteP = NULL;
  45.     Rect                 itemRect, moveBoundsRect;
  46.     short                 itemHit, itemType;
  47.     Handle                itemHandle;
  48. #if SW_PPC
  49.     ModalFilterUPP spriteWorldDialogFilter;
  50.     static UserItemUPP    procForVersUserItem = nil;
  51. #else
  52.     ModalFilterProcPtr     spriteWorldDialogFilter;
  53.     ProcPtr             procForVersUserItem;
  54. #endif
  55.  
  56. #if SW_PPC
  57.     if ( procForVersUserItem == nil )
  58.         procForVersUserItem = NewUserItemProc(versUserItem);
  59. #else
  60.     procForVersUserItem = (ProcPtr)versUserItem;
  61. #endif
  62.  
  63.     aboutDialogP = GetNewDialog(kAboutDialogID, NULL, (WindowPtr)-1L);
  64.     
  65.     GetDItem ( aboutDialogP, 6, &itemType, &itemHandle, &itemRect );
  66.     SetDItem (aboutDialogP, 6, itemType, (Handle)procForVersUserItem, &itemRect);
  67.     
  68.     if (aboutDialogP != NULL)
  69.     {
  70.         GetPort(&savePort);
  71.         SetPort(aboutDialogP);
  72.  
  73.             // create the sprite world
  74.         err = SWCreateSpriteWorldFromWindow(&spriteWorldP, (CWindowPtr)aboutDialogP, NULL, NULL);
  75.     
  76.         if (err == noErr)
  77.         {    
  78.                 // create the sprite layer
  79.             err = SWCreateSpriteLayer(&spriteLayerP);
  80.         }
  81.  
  82.         if (err == noErr)
  83.         {
  84.             err = SWCreateSpriteFromCicnResource(
  85.                 spriteWorldP,
  86.                 &earthSpriteP, 
  87.                 NULL, 
  88.                 kEarthCIconID, 
  89.                 1, 
  90.                 kRegionMask);
  91.         }
  92.  
  93.         if (err == noErr)
  94.         {
  95.             err = SWCreateSpriteFromCicnResource(
  96.                 spriteWorldP, 
  97.                 &shadowSpriteP, 
  98.                 NULL, 
  99.                 kShadowCIconID,
  100.                 kNumberOfShadowFrames, 
  101.                 kRegionMask);
  102.         }
  103.  
  104.         if (err == noErr)
  105.         {
  106.             shadowSpriteP->userData = (long)earthSpriteP;
  107.  
  108.             SWAddSprite(spriteLayerP, shadowSpriteP);
  109.             SWAddSprite(spriteLayerP, earthSpriteP);
  110.             SWAddSpriteLayer(spriteWorldP, spriteLayerP);
  111.  
  112.             GetDItemRect(aboutDialogP, 4, &itemRect);
  113.             moveBoundsRect = aboutDialogP->portRect;
  114.             moveBoundsRect.bottom = itemRect.top + ((itemRect.bottom-itemRect.top)/2);
  115.  
  116.                 // set the sprite’s movement characteristics
  117.             SWSetSpriteMoveBounds(earthSpriteP, &moveBoundsRect);
  118.             SWSetSpriteMoveDelta(earthSpriteP, 0, kInitialSpeed);
  119.             SWSetSpriteMoveProc(earthSpriteP, EarthMoveProc);
  120.             SWSetSpriteMoveTime(earthSpriteP, 55);
  121.             SWSetSpriteMoveTime(shadowSpriteP, -1);        // never move
  122.             SWSetSpriteFrameTime(shadowSpriteP, 0);
  123.             SWSetSpriteFrameRange(shadowSpriteP, 0, 4);
  124.             SWSetSpriteFrameProc(shadowSpriteP, ShadowFrameProc);
  125.             SWSetCurrentFrameIndex(shadowSpriteP, 2);
  126.  
  127.                 // set the sprite’s initial location
  128.             SWSetSpriteLocation(shadowSpriteP, itemRect.left, itemRect.top);
  129.             GetDItemRect(aboutDialogP, 5, &itemRect);
  130.             SWSetSpriteLocation(earthSpriteP, itemRect.left, itemRect.top);
  131.  
  132.             ((WindowPeek)aboutDialogP)->refCon = (long)spriteWorldP;
  133.  
  134.             ShowWindow(aboutDialogP);
  135.             SetPort(aboutDialogP);
  136.             DrawDialog(aboutDialogP);
  137.             EraseRect(&itemRect);
  138.             OutlineDefaultButton(aboutDialogP, ok);
  139.  
  140.             SWLockSpriteWorld(spriteWorldP);
  141.  
  142.             CopyBits(&aboutDialogP->portBits,
  143.                         (BitMap*)spriteWorldP->backFrameP->framePix,
  144.                         &aboutDialogP->portRect,
  145.                         &aboutDialogP->portRect,
  146.                         srcCopy, NULL);
  147.  
  148.  
  149.             spriteWorldDialogFilter = NewModalFilterProc(AboutDialogFilter);
  150.  
  151.             do
  152.             {
  153.                 ModalDialog(spriteWorldDialogFilter, &itemHit);
  154.             } while (itemHit != ok);
  155.  
  156. #if SW_PPC
  157.             DisposeRoutineDescriptor(spriteWorldDialogFilter);
  158. #endif
  159.  
  160.             SWUnlockSpriteWorld(spriteWorldP);
  161.         }
  162.  
  163.         DisposeDialog(aboutDialogP);
  164.         if (spriteWorldP != NULL)
  165.             SWDisposeSpriteWorld(spriteWorldP);
  166.  
  167.         SetPort(savePort);
  168.  
  169.         if (err != noErr)
  170.         {
  171.             ErrorAlert(err, kUnknownErrorStringIndex);
  172.         }
  173.     }
  174.     else
  175.     {
  176.         ErrorAlert(ResError(), kCantFindResourceStringIndex);
  177.     }
  178. }
  179.  
  180.  
  181. ///--------------------------------------------------------------------------------------
  182. // EarthMoveProc
  183. ///--------------------------------------------------------------------------------------
  184.  
  185. SW_FUNC void EarthMoveProc(
  186.     SpritePtr srcSpriteP)
  187. {
  188.         // gravity
  189.     if (srcSpriteP->vertMoveDelta < (short)kMaxSpeed)
  190.         srcSpriteP->vertMoveDelta++;
  191.  
  192.     SWOffsetSprite( srcSpriteP, 0, srcSpriteP->vertMoveDelta );
  193.         // bounce
  194.     if (srcSpriteP->destFrameRect.bottom > srcSpriteP->moveBoundsRect.bottom)
  195.     {
  196.         srcSpriteP->vertMoveDelta = -srcSpriteP->vertMoveDelta;
  197.         
  198.         SWMoveSprite( srcSpriteP, srcSpriteP->destFrameRect.left, 
  199.             srcSpriteP->moveBoundsRect.bottom -
  200.             (srcSpriteP->curFrameP->frameRect.bottom - 
  201.             srcSpriteP->curFrameP->frameRect.top));
  202.     }
  203. }
  204.  
  205.  
  206. ///--------------------------------------------------------------------------------------
  207. // ShadowFrameProc
  208. ///--------------------------------------------------------------------------------------
  209.  
  210. SW_FUNC void ShadowFrameProc(
  211.     SpritePtr srcSpriteP,
  212.     FramePtr curFrameP,
  213.     long* curFrameIndex)
  214. {
  215.  
  216.     SpritePtr earthSpriteP = (SpritePtr)srcSpriteP->userData;
  217.     short distanceFromTop = earthSpriteP->destFrameRect.top - srcSpriteP->moveBoundsRect.top;
  218.     short height = earthSpriteP->moveBoundsRect.bottom - earthSpriteP->moveBoundsRect.top;
  219.  
  220.     *curFrameIndex = (long)((distanceFromTop * 5) / height);
  221.  
  222.         // lets not index past the last frame!
  223.         // (even though SpriteWorld will not allow it to happen)
  224.     if (*curFrameIndex > 4L) *curFrameIndex = 4L;
  225. }
  226.  
  227.  
  228. ///--------------------------------------------------------------------------------------
  229. // AboutDialogFilter
  230. ///--------------------------------------------------------------------------------------
  231.  
  232. pascal Boolean AboutDialogFilter(
  233.     DialogPtr aboutDialogP,
  234.     EventRecord *event,
  235.     short *itemHit)
  236. {
  237.     Boolean eventHandled = false;
  238.     SpriteWorldPtr spriteWorldP = (SpriteWorldPtr)((WindowPeek)aboutDialogP)->refCon;
  239.  
  240.     switch (event->what)
  241.     {
  242.         case nullEvent:
  243.         {
  244.                 // run the about box animation
  245.             SWProcessSpriteWorld(spriteWorldP);
  246.             SWAnimateSpriteWorld(spriteWorldP);
  247.  
  248.                 // keep the title animation going!
  249.             SpriteTestIdle(gSpriteTestP);
  250.             break;
  251.         }
  252.  
  253.         case keyDown:
  254.         case autoKey:
  255.         {
  256.             char key = (char)(event->message & charCodeMask);
  257.  
  258.             if (key == kReturnChar || (key == kEnterChar))
  259.             {
  260.                 *itemHit = ok;
  261.                 eventHandled = true;
  262.                 ClickDialogButton(aboutDialogP, ok);
  263.             }
  264.  
  265.             break;
  266.         }
  267.  
  268.         case updateEvt:
  269.         {
  270.             if ((DialogPtr)event->message == aboutDialogP)
  271.             {
  272.                 SetPort(aboutDialogP);
  273.                 BeginUpdate(aboutDialogP);
  274.  
  275.                 SWUpdateSpriteWorld(spriteWorldP, true);
  276.                 
  277.                 EndUpdate(aboutDialogP);
  278.                 eventHandled = true;
  279.             }
  280.             else
  281.             {
  282.                 HandleUpdateEvent((WindowPtr)event->message);
  283.             }
  284.  
  285.             break;
  286.         }
  287.     }
  288.  
  289.     return eventHandled;
  290. }
  291.  
  292. /**********************************  versUserItem  *******/
  293. pascal void versUserItem (DialogPtr theDialog, short itemNo)
  294.  
  295. {
  296.     Rect            itemRect;
  297.     short            itemType;
  298.     Handle            itemHandle;
  299.     unsigned long    versNum;
  300.     short            majorNum;
  301.     short            betaNum;
  302.     char            versDigit1, versDigit2, versDigit3;
  303.     
  304.     
  305.     
  306.     GetDItem ( theDialog, itemNo, &itemType, &itemHandle, &itemRect );
  307.     
  308.     versNum = SWGetSpriteWorldVersion();
  309.     
  310.     majorNum = HiWord( versNum );
  311.     majorNum>>=8;
  312.     versDigit1 = majorNum | '0';
  313.     majorNum = (HiWord( versNum ))&0x00FF;
  314.     majorNum >>=4;
  315.     versDigit2 = majorNum | '0';
  316.     majorNum = (HiWord( versNum ))&0x000F;
  317.     versDigit3 = majorNum | '0';
  318.     
  319.     MoveTo( itemRect.left, itemRect.top + 9 );
  320.     TextFont(geneva);
  321.     TextSize(9);
  322.     DrawChar( versDigit1 );
  323.     DrawChar( '.' );
  324.     DrawChar( versDigit2 );
  325.     if ( versDigit3 > '0' )
  326.     {
  327.         DrawChar( '.' );
  328.         DrawChar( versDigit3 );
  329.     }
  330.  
  331.     betaNum = LoWord( versNum );
  332.     if ( betaNum )
  333.     {
  334.         ForeColor( blueColor );
  335.         DrawString( "\p beta" );
  336.         DrawChar( '0'+betaNum );
  337.         ForeColor( blackColor );
  338.     }
  339.     DrawString( "\p by" );
  340.     TextFace( 0 );
  341.     TextFont(systemFont);
  342.     TextSize(12);
  343. }
  344.  
  345.  
  346.